package com.luyun.frogpay.base.baseclass;

import android.content.Context;

import com.luyun.frogpay.base.application.BaseApplication;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author 无人区
 * @date 2019/10/27 20:21
 * @desc:
 */
public class BasePresenter<T extends BaseContract.BaseView> implements BaseContract.BasePresenter<T> {

    protected T view;

    protected Context context = BaseApplication.context;

    @Override
    public void attachView(T view) {
        this.view = view;
    }

    @Override
    public void detachView() {
        if (null != view){
            view = null;
        }
    }

    /**
     * 把数组所有元素排序，并按照“参数=参数值”的模式用“&”字符拼接成字符串
     *
     * @param params
     * 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    protected static String buildRequestDataForSign(Map<String, Object> params, String safeKey) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            Object value = params.get(key);
            if (i == keys.size() - 1) {// 拼接时，不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }
        return prestr + "&key=" + safeKey;
    }
}
